Passed
Push — develop ( e6e477...3b1736 )
by Paul
03:05
created

pollux.tabs.init   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
nc 2
nop 2
1
/** global: wp, CodeMirror */
2
3
var pollux = {
4
	editors: {},
5
	media: {
6
		featured: {},
7
	},
8
	metabox: {},
9
	tabs: {},
10
};
11
12
/**
13
 * @return bool
14
 */
15
pollux.classListAction = function( bool )
16
{
17
	return bool ? 'add' : 'remove';
18
};
19
20
/**
21
 * @return void
22
 */
23
pollux.media.featured.init = function()
24
{
25
	jQuery( '#postimagediv' )
26
	.on( 'click', '#pollux-set-featured', function( ev ) {
27
		ev.preventDefault();
28
		wp.media.view.settings.post.featuredImageId = Math.round( jQuery( '#featured' ).val() );
29
		pollux.media.featured.frame = wp.media.featuredImage.frame;
30
		pollux.media.featured.frame().open();
31
	})
32
	.on( 'click', '#pollux-remove-featured', function( ev ) {
33
		ev.preventDefault();
34
		pollux.media.featured.set(-1);
35
	});
36
};
37
38
/**
39
 * @return void
40
 */
41
pollux.media.featured.select = function()
42
{
43
	if( !wp.media.view.settings.post.featuredImageId )return;
44
	var selection = this.get( 'selection' ).single();
45
	pollux.media.featured.set( selection ? selection.id : -1 );
46
};
47
48
/**
49
 * @return void
50
 */
51
pollux.media.featured.set = function( id )
52
{
53
	wp.media.view.settings.post.featuredImageId = Math.round( id );
54
	wp.media.post( 'pollux/archives/featured/html', {
55
		_wpnonce: document.querySelector( '#_wpnonce' ).value,
56
		post_type: document.querySelector( '#archive-type' ).value,
57
		thumbnail_id: id,
58
	}).done( function( html ) {
59
		document.querySelector( '#postimagediv > .inside' ).innerHTML = html;
60
	});
61
};
62
63
/**
64
 * @return bool
65
 */
66
pollux.metabox.hasValue = function( el )
67
{
68
	if( el.type === 'checkbox' ) {
69
		return el.checked === true;
70
	}
71
	return el.value !== '';
72
};
73
74
/**
75
 * @return void
76
 */
77
pollux.metabox.init = function()
78
{
79
	var depends = document.querySelectorAll( '.rwmb-input [data-depends]' );
80
	[].forEach.call( depends, function( el ) {
81
		var dependency = pollux.metabox.setVisibility( el );
82
		var event = dependency.type === 'checkbox' ? 'change' : 'keyup';
83
		dependency.addEventListener( event, function() {
84
			pollux.metabox.setVisibility( el );
85
		});
86
	});
87
};
88
89
/**
90
 * @return element
91
 */
92
pollux.metabox.setVisibility = function( el )
93
{
94
	var dependency = document.getElementById( el.getAttribute( 'data-depends' ));
95
	var action = pollux.classListAction( !pollux.metabox.hasValue( dependency ));
96
	el.closest( '.rwmb-field' ).classList[action]( 'hidden' );
97
	return dependency;
98
};
99
100
/**
101
 * @return void
102
 */
103
pollux.tabs.init = function()
104
{
105
	pollux.tabs.tabs = document.querySelectorAll( '.pollux-tabs a' );
106
	pollux.tabs.views = document.querySelectorAll( '#pollux-config .table' );
107
108
	[].forEach.call( pollux.tabs.tabs, function( tab, index ) {
109
		var active = location.hash ? tab.getAttribute( 'href' ) === location.hash : index === 0;
110
		if( active ) {
111
			pollux.tabs.setTab( tab );
112
		}
113
		tab.addEventListener( 'click', pollux.tabs.onClick );
114
		tab.addEventListener( 'touchend', pollux.tabs.onClick );
115
	});
116
};
117
118
/**
119
 * @return void
120
 */
121
pollux.tabs.onClick = function( ev )
122
{
123
	ev.preventDefault();
124
	this.blur();
125
	var hash = this.getAttribute( 'href' );
126
	var view = document.querySelector( '#pollux-config ' + hash );
127
	pollux.tabs.setTab( this );
128
	view.removeAttribute( 'id' );
129
	location.hash = hash;
130
	view.setAttribute( 'id', hash.slice( 1 ));
131
};
132
133
/**
134
 * @return void
135
 */
136
pollux.tabs.setTab = function( el )
137
{
138
	[].forEach.call( pollux.tabs.tabs, function( tab, index ) {
139
		var action = pollux.classListAction( tab === el );
140
		if( action === 'add' ) {
141
			pollux.tabs.setView( index );
142
		}
143
		tab.classList[action]( 'nav-tab-active' );
144
	});
145
};
146
147
/**
148
 * @return void
149
 */
150
pollux.tabs.setView = function( idx )
151
{
152
	[].forEach.call( pollux.tabs.views, function( view, index ) {
153
		var action = pollux.classListAction( index !== idx );
154
		view.classList[action]( 'ui-tabs-hide' );
155
	});
156
};
157
158
pollux.editors.init = function()
159
{
160
	[].forEach.call( document.querySelectorAll( '.pollux-code' ), function( editor ) {
161
		var cmeditor = CodeMirror.fromTextArea( editor, {
162
			gutters: ['CodeMirror-lint-markers'],
163
			highlightSelectionMatches: { wordsOnly: true },
164
			indentWithTabs: false,
165
			lineNumbers: true,
166
			lint: true,
167
			mode: 'text/yaml',
168
			showInvisibles: true,
169
			showTrailingSpace: true,
170
			styleActiveLine: true,
171
			tabSize: 2,
172
			theme: 'pollux',
173
			viewportMargin: Infinity,
174
		});
175
		cmeditor.setOption( 'extraKeys', {
176
			Tab: function( cm ) {
177
				var spaces = Array( cm.getOption( 'indentUnit' ) + 1 ).join( ' ' );
178
				cm.replaceSelection( spaces );
179
			},
180
		});
181
	});
182
};
183
184
jQuery(function() {
185
	pollux.editors.init();
186
	pollux.media.featured.init();
187
	pollux.metabox.init();
188
	pollux.tabs.init();
189
});
190